﻿package TestSocket1;
都是粉色为
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * 客户端
 * 
 * @author Administrator
 *
 */
public class Client {

	public static void main(String[] args) throws UnknownHostException, IOException {
		Scanner sc = new Scanner(System.in);
		// 创建一个客户端socket
		Socket socket = null;
		while (true) {
			socket = new Socket("localhost", 8888);
			// 向服务器端传递信息
			OutputStream ots = socket.getOutputStream();
			PrintWriter pw = new PrintWriter(ots);
			System.out.println("请输入信息");
			String words = sc.nextLine();
			pw.write(words);
			pw.flush();
			// 关闭输出流
			socket.shutdownOutput();
			// 获取服务器端传递的数据
			InputStream is = socket.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String info = null;
			while ((info = br.readLine()) != null) {
				System.out.println("我是客户端，服务器说：" + info);
			}
			socket.shutdownInput();// 关闭输入流
			if (words.equals("0000")) {
				// 关闭资源
				br.close();
				isr.close();
				is.close();
				pw.close();
				ots.close();
				socket.close();
			}
		}

	}

}
范德萨发顺丰